package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.CommodityManagementAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.CommodityManagementModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/24.
 * 商品管理
 */

public class CommodityManagementActivity extends Activity implements View.OnClickListener, Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.listview)
    ListView listview;

    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;

    @Bind(R.id.bottom_options)//添加商品
            LinearLayout bottom_options;

    Context context;
    List<CommodityManagementModel> ManagementModelList = new ArrayList<>();//原有数据
    List<CommodityManagementModel> buycarlist = new ArrayList<>();//组合数据
    List<Integer> listint = new ArrayList<>();//数据
    CommodityManagementAdapter adapter;
    String merchanttype = "";//类型
    String u_id = "";
    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、获取数据 1、下架 2、删除
    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_management);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {


        retur.setOnClickListener(this);
        bottom_options.setOnClickListener(this);

        listview.setOnItemClickListener(clickListener);
        if (getIntent() != null) {
            u_id = getIntent().getStringExtra("id");
        }
      //  u_id = "16";
        initData();
    }

    AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            processIntent("2", buycarlist.get(i).getId(), i);
        }
    };

    private void initData() {
        MineFragmentModel mineFragmentModel = UserInfoUtils.readUserInfo(this);
        merchanttype = mineFragmentModel.getMerchanttype();
     //   merchanttype = "1";
        adapter = new CommodityManagementAdapter(context, merchanttype);
        listview.setAdapter(adapter);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.bottom_options://添加商品
                processIntent("0", u_id, 0);
                break;

        }
    }

    //跳转页面
    public void processIntent(String str, String id, int i) {
        Intent intent = new Intent();
        if (merchanttype.equals("1")) {//买房商家

            intent.setClass(context, AddThePropertyActivity.class);
            intent.putExtra("pos", str);
            if (!str.equals("0")) {
                intent.putExtra("id", id);
            }
            intent.putExtra("u_id", u_id);
            startActivity(intent);
        } else if (merchanttype.equals("2")) {//购车商家

            intent.setClass(context, AddCarPurchasesGoodsActivity.class);
            intent.putExtra("pos", str);
            if (!str.equals("0")) {
                intent.putExtra("ints", i);
                intent.putExtra("data", (Serializable) buycarlist);
            }
            intent.putExtra("id", id);
            startActivity(intent);
        } else if (merchanttype.equals("3")) {//干洗商家
            intent.setClass(context, AddTheGoodsActivity.class);
            intent.putExtra("pos", str);
            if (!str.equals("0")) {
                intent.putExtra("ints", i);
                intent.putExtra("data", (Serializable) buycarlist);
            }
            intent.putExtra("id", id);
            startActivity(intent);
        } else if (merchanttype.equals("4")) {//开锁商家

        } else if (merchanttype.equals("5")) {//果蔬商家
            intent.setClass(context, AddTheGoodsActivity.class);
            intent.putExtra("pos", str);
            if (!str.equals("0")) {
                intent.putExtra("ints", i);
                intent.putExtra("data", (Serializable) buycarlist);
            }
            intent.putExtra("id", id);
            startActivity(intent);
        } else if (merchanttype.equals("6")) {//苦力商家

        } else if (merchanttype.equals("7")) {//快递商家

        } else if (merchanttype.equals("8")) {//家政商家

            intent.setClass(context, AddTheServiceActivity.class);
            intent.putExtra("pos", str);
            if (!str.equals("0")) {
                intent.putExtra("ints", i);
                intent.putExtra("data", (Serializable) ManagementModelList);
            }
            intent.putExtra("id", id);
            startActivity(intent);
        } else if (merchanttype.equals("9")) {//洗车商家
            intent.setClass(context, CarAddGoodsActivity.class);
            intent.putExtra("pos", str);
            if (!str.equals("0")) {
                intent.putExtra("ints", i);
                intent.putExtra("data", (Serializable) ManagementModelList);
            }
            intent.putExtra("id", id);
            startActivity(intent);
        }

    }

    //删除
    public void processdeleting(final String id) {
        alertDialog = Dialog.phonedialog(context, "是否删除该商品", "删除商品", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Sundry.isOpenNetwork(context)) {
                    mDialog = new MyProgressDialog(context);
                    Judge_interface = "2";

                    if (merchanttype.equals("1")) {//买房商家
                        HttpClient.delbulding(id, CommodityManagementActivity.this);
                    } else {
                        String Data = "";
                        if (merchanttype.equals("2")) {//购车商家
                            Data = Constant.CARBUYINGDEL;
                        } else if (merchanttype.equals("3")) {//干洗商家
                            Data = Constant.CLEANINGDEL;
                        } else if (merchanttype.equals("5")) {//果蔬商家
                            Data = Constant.VEGETABLESDEL;
                        } else if (merchanttype.equals("8")) {//家政商家
                            Data = Constant.HOUSEKEEPINGDEL;
                        } else if (merchanttype.equals("9")) {//洗车商家
                            Data = Constant.CARWASHDEL;
                        }
                        HttpClient.del(Data, id, CommodityManagementActivity.this);
                    }
                } else {
                    ToastUtils.Toastshow(context, getResources().getString(R.string.network));
                }
                alertDialog.dismiss();
            }
        }, "确定", "取消");
    }

    //上架下架
    public void processshelves(String id, String audit) {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "1";


            if (merchanttype.equals("1")) {//买房商家
                String aud = "";
                if (audit.equals("3")) {
                    aud = "2";
                } else {
                    aud = "3";
                }
                HttpClient.operatio(id, aud, this);
            } else {
                String Data = "";
                if (merchanttype.equals("2")) {//购车商家
                    Data = Constant.CARBUYINGUNDERCARRIAGE;
                } else if (merchanttype.equals("3")) {//干洗商家
                    Data = Constant.CLEANINGUNDERCARRIAGE;
                } else if (merchanttype.equals("5")) {//果蔬商家
                    Data = Constant.VEGETABLESUNDERCARRIAGE;
                } else if (merchanttype.equals("8")) {//家政商家
                    Data = Constant.HOUSEKEEPINGUNDERCARRIAGE;
                } else if (merchanttype.equals("9")) {//洗车商家
                    Data = Constant.CARWASHUNDERCARRIAGE;
                }
                HttpClient.Cleaning(Data, id, this);
            }
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //驳回
    public void processreject() {
        startActivity(new Intent(context, MessageActivity.class));
    }
    //户型
    public void processmodel(String id) {
       Intent intent= new Intent(context, FamilyManagementActivity.class);
        intent.putExtra("id",id);
        startActivity(intent);
    }
    @Override
    protected void onResume() {
        super.onResume();
        processinitData();
    }

    //初始数据
    private void processinitData() {
        if (Sundry.isOpenNetwork(context)) {
            if (Judge_interface.equals("0")){
                mDialog = new MyProgressDialog(context);
            }
            Judge_interface = "0";

            if (merchanttype.equals("1")) {//买房商家
                HttpClient.Bridalchamber(u_id, this);
            } else {
                String Data = "";
                if (merchanttype.equals("2")) {//购车商家
                    Data = Constant.MERCHANTREGULATORBUYACAR;

                } else if (merchanttype.equals("3")) {//干洗商家
                    Data = Constant.MERCHANTREGULATORCLEANING;

                } else if (merchanttype.equals("5")) {//果蔬商家
                    Data = Constant.MERCHANTREGULATORVEGETABLES;

                } else if (merchanttype.equals("8")) {//家政商家

                    Data = Constant.MERCHANTREGULATORHOUSEKEEPING;
                } else if (merchanttype.equals("9")) {//洗车商家

                    Data = Constant.MERCHANTREGULATORCARWASH;
                }
                HttpClient.Merchantregulator(Data, u_id, this);
            }
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }


    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
            if (Judge_interface.equals("0")) {//数据
                adapter.adddeleting();
            }
            if (error.equals("200")) {
                String content = getnote.get(0).getResult();
                if (Judge_interface.equals("0")) {//数据
                    available_layout.setVisibility(View.GONE);
                    if (merchanttype.equals("1")) {//买房商家
                        List<CommodityManagementModel> getcleaning = JsonTools.getbuyhouse(content);
                        if (getcleaning != null || getcleaning.size() > 0) {
                            processbuyhouse(getcleaning);
                        }

                    } else if (merchanttype.equals("2")) {//购车商家
                        List<CommodityManagementModel> getcleaning = JsonTools.getcar(content);
                        if (getcleaning != null || getcleaning.size() > 0) {
                            processcar(getcleaning);
                        }

                    } else if (merchanttype.equals("3") || merchanttype.equals("5")) {//干洗和蔬果商家
                        List<CommodityManagementModel> getcleaning = JsonTools.getcleaning(content);
                        if (getcleaning != null || getcleaning.size() > 0) {
                            processDryCleaning(getcleaning);
                        }

                    } else if (merchanttype.equals("8")) {//家政商家

                        List<CommodityManagementModel> getcleaning = JsonTools.gethomemaking(content);
                        if (getcleaning != null || getcleaning.size() > 0) {
                            processhomemaking(getcleaning);
                        }
                    } else if (merchanttype.equals("9")) {//洗车商家
                        List<CommodityManagementModel> getcleaning = JsonTools.getcleaning(content);
                        if (getcleaning != null || getcleaning.size() > 0) {
                            processWashcar(getcleaning);
                        }
                    }
                } else if (Judge_interface.equals("1")) {//上架下架
                    ToastUtils.show(context, mssage);
                    processinitData();
                } else if (Judge_interface.equals("2")) {//删除
                    ToastUtils.show(context, mssage);
                    processinitData();
                }
            } else if (error.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, mssage);
            } else {
                if (Judge_interface.equals("0")) {//数据
                    available_layout.setVisibility(View.VISIBLE);
                }
                ToastUtils.show(context, mssage);
            }
        }

    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {

        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {//数据
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));

    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {//数据
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }

    //干洗和蔬果商家
    private void processDryCleaning(List<CommodityManagementModel> getcleaning) {
        buycarlist.clear();
        listint.clear();
        List<CommodityManagementModel> commodityManagementModels;
        int t = 0;

        boolean isFirst;
        for (int i = 0; i < getcleaning.size(); i++) {
            isFirst=true;
            commodityManagementModels = JsonTools.getcleaningList(getcleaning.get(i).getList());
            if (commodityManagementModels != null || commodityManagementModels.size() > 0) {
                for (int j = 0; j < commodityManagementModels.size(); j++) {
                    CommodityManagementModel model = new CommodityManagementModel();
                    CommodityManagementModel commodityModel = commodityManagementModels.get(j);
                    model.setNametype(getcleaning.get(i).getName());
                    model.setId(commodityModel.getId());//产品ID
                    model.setName(commodityModel.getName());//产品名称
                    model.setPrice(commodityModel.getPrice());//价格
                    model.setAudit(commodityModel.getAudit());//商品状态
                    model.setAdd_time(commodityModel.getAdd_time());//上传时间
                    model.setIsdisplay(commodityModel.getIsdisplay());//是否上下架
                    model.setOrder(commodityModel.getOrder());//订单数量
                    model.setClassid(commodityModel.getClassid());//类别id
                    model.setIntroduction(commodityModel.getIntroduction());//简介
                    model.setReject(commodityModel.getReject());//驳回理由
                    model.setRejecttime(commodityModel.getRejecttime());//驳回时间
                    model.setImg(commodityModel.getImg());//图片
                    buycarlist.add(model);

                    if (isFirst){
                        listint.add(t);
                        isFirst=false;
                    }else {
                        listint.add(0);
                    }
                    t++;
                }
            }
        }
        adapter.addItem(buycarlist, listint);
    }

    //洗车商家
    private void processWashcar(List<CommodityManagementModel> getcleaning) {
        buycarlist.clear();
        listint.clear();
        ManagementModelList.clear();
        List<CommodityManagementModel> commodityManagementModels;
        for (int i = 0; i < getcleaning.size(); i++) {
            commodityManagementModels = JsonTools.getWashcarList(getcleaning.get(i).getList());
            if (commodityManagementModels != null || commodityManagementModels.size() > 0) {
                for (int j = 0; j < commodityManagementModels.size(); j++) {
                    CommodityManagementModel model = new CommodityManagementModel();
                    CommodityManagementModel commodityModel = commodityManagementModels.get(j);
                    model.setId(commodityModel.getId());//产品ID
                    model.setName(getcleaning.get(i).getName());//产品名称
                    model.setPrice(commodityModel.getLimousinediudiuprice());//价格
                    model.setAudit(commodityModel.getAudited());//商品状态
                    model.setAdd_time(commodityModel.getApplytime());//上传时间
                    model.setIsdisplay(commodityModel.getUpframe());//是否上下架
                    model.setOrder(commodityModel.getOrder());//订单数量
                    model.setIntroduction(commodityModel.getIntro());//简介
                    model.setReject(commodityModel.getReject());//驳回理由
                    model.setRejecttime(commodityModel.getRejecttime());//驳回时间
                    buycarlist.add(model);
                    listint.add(i);


                    commodityModel.setNametype(getcleaning.get(i).getName());
                    ManagementModelList.add(commodityModel);
                }
            }
        }
        adapter.addItem(buycarlist, listint);
    }

    //购车商家
    private void processcar(List<CommodityManagementModel> getcleaning) {
        buycarlist.clear();

        for (int j = 0; j < getcleaning.size(); j++) {
            CommodityManagementModel model = new CommodityManagementModel();
            CommodityManagementModel commodityModel = getcleaning.get(j);
            model.setId(commodityModel.getId());//产品ID
            model.setName(commodityModel.getBrand());//产品名称
            model.setPrice(commodityModel.getSellingprice());//价格
            model.setAudit(commodityModel.getAudited());//商品状态
            model.setAdd_time(commodityModel.getApplytime());//上传时间
            model.setIsdisplay(commodityModel.getUpframe());//是否上下架
            model.setOrder(commodityModel.getOrder());//订单数量
            model.setIntroduction(commodityModel.getIntro());//简介
            model.setReject(commodityModel.getReject());//驳回理由
            model.setRejecttime(commodityModel.getRejecttime());//驳回时间
            model.setSelected(commodityModel.getSelected());//选中
            buycarlist.add(model);
        }

        adapter.addItem(buycarlist, listint);
    }

    //家政商家
    private void processhomemaking(List<CommodityManagementModel> getcleaning) {
        buycarlist.clear();
        ManagementModelList.clear();
        for (int j = 0; j < getcleaning.size(); j++) {
            CommodityManagementModel model = new CommodityManagementModel();
            CommodityManagementModel commodityModel = getcleaning.get(j);
            model.setId(commodityModel.getId());//产品ID
            model.setName(commodityModel.getClassname());//产品名称
            model.setPrice(commodityModel.getPrice());//价格
            model.setAudit(commodityModel.getAudit());//商品状态
            model.setAdd_time(commodityModel.getAdd_time());//上传时间
            model.setIsdisplay(commodityModel.getIsdisplay());//是否上下架
            model.setOrder(commodityModel.getOrder());//订单数量
            model.setProductid(commodityModel.getTimetype());
            model.setIntroduction(commodityModel.getSynopsis());//简介
            model.setReject(commodityModel.getReject());//驳回理由
            model.setRejecttime(commodityModel.getRejecttime());//驳回时间
            model.setServicetime(commodityModel.getServicetime());//单位
            buycarlist.add(model);
            ManagementModelList.add(commodityModel);
        }

        adapter.addItem(buycarlist, listint);
    }

    //买房商家
    private void processbuyhouse(List<CommodityManagementModel> getcleaning) {
        buycarlist.clear();
        for (int j = 0; j < getcleaning.size(); j++) {
            CommodityManagementModel model = new CommodityManagementModel();
            CommodityManagementModel commodityModel = getcleaning.get(j);
            model.setId(commodityModel.getId());//产品ID
            model.setName(commodityModel.getTitle());//产品名称
            model.setPrice(commodityModel.getPrice()); //价格
            model.setAudit(commodityModel.getIs_play()); //商品状态
            model.setAdd_time(commodityModel.getAddtime());//上传时间
            model.setIsdisplay(commodityModel.getIs_audit());//是否上下架
            model.setOrder(commodityModel.getNumber());//订单数量
            model.setProductid(commodityModel.getType());
            model.setIntroduction(commodityModel.getAddress()); //简介
            model.setReject(commodityModel.getReject());//驳回理由
            model.setRejecttime(commodityModel.getRejecttime());//驳回时间
            buycarlist.add(model);
        }

        adapter.addItem(buycarlist, listint);
    }
}
